﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Plane.Properties;

namespace Plane
{
    public class MyPlane
    {
        public static int x = 180;
        public static int y = 530;//坐标
        public static int health = 100; //血量

        private static (string Password, int Money, int HighestScore, int RecoveryAmount, int BulletAmount, string PlaneSkin, string KeyUp, string KeyDown, string KeyLeft, string KeyRight, string KeyFire, int SoundVolume) User;
        private string username;
        public MyPlane(string username)
        {
            this.username = username; 
            User = UserDbHelper.GetUserInformationByUsername(username);
        }

        private const int PLANE_OFFSET = 12;//移动速度

        //我方飞机图片
        public static Image myPlaneImgS;
        public static Image myPlaneImgL;
        public static Image myPlaneImgR;
        public static Image myPlaneImg;
        static List<Keys> keys = new List<Keys>();//键盘键列表，用于控制飞机移动
        static Image gameOver = Resources.gameover;

        public static int isGetGun = 0;//得到增强子弹
        public static bool isBloodCollected = false;//是否得到bloodbox的标志
        public static bool isGameOver = false; //游戏是否结束的标志
        public static int score = 0;      //得分
        public static int bloodRecovery=10;
        public static bool usedBlood = false;
        /// 显示我方飞机
        
        public static void MyPlaneShow(Graphics g)
        {
            if (health > 0)
            {
                g.DrawImage(myPlaneImg, x, y);
            }
            else if (health <= 0 || score <= 0)
            {
                isGameOver = true;
                g.DrawImage(myPlaneImg, 0, -300);
                g.DrawImage(gameOver, 10, 260);
            }
            if (isBloodCollected)
            {
                if (health <= 100 - bloodRecovery)
                    health += bloodRecovery;
                else
                    health = 100;

                isBloodCollected = false; // 恢复后清空捡到标志
            }
        }

        
        /// 松开键盘键
        
        public static void Keyup(Keys key)
        {
            if (keys.Contains(key))
            {
                keys.Remove(key);
            }
        }

        /// 按下键盘键
       
        public static void Keydown(Keys key)
        {
            if (!keys.Contains(key))
            {
                keys.Add(key);
            }
        }
        
        /// 判断按键是否被按下
        
        public static bool IsKeyDown(Keys key)
        {
            return keys.Contains(key);
        }
        
       
        /// 用键盘控制我方飞机移动
        
        public static void MyPlaneMove()
        {
            if(isGameOver)
            {
                return;
            }
            Enum.TryParse(User.KeyLeft, ignoreCase: true, out Keys leftKey);
            Enum.TryParse(User.KeyRight, ignoreCase: true, out Keys rightKey);
            Enum.TryParse(User.KeyUp, ignoreCase: true, out Keys upKey);
            Enum.TryParse(User.KeyDown, ignoreCase: true, out Keys downKey);
            
                if (IsKeyDown(leftKey))
                {
                    myPlaneImg = myPlaneImgL;
                    if (x < 5)
                        x = 5;
                    x -= PLANE_OFFSET;
                }
                if (IsKeyDown(rightKey))
                {
                    myPlaneImg = myPlaneImgR;
                    if (x > 370)
                        x = 370;
                    x += PLANE_OFFSET;
                }
                if (IsKeyDown(upKey))
                {
                    if (y < 5)
                        y = 5;
                    y -= PLANE_OFFSET;
                }
                if (IsKeyDown(downKey))
                {
                    if (y > 530)
                        y = 530;
                    y += PLANE_OFFSET;
                }
            
        }
        //重制游戏
        public static void Reset()
        {
            x = 180;
            y = 530;
            health = 100;
            score = 0;
            isGetGun = User.BulletAmount;
            isBloodCollected = false;
            isGameOver = false;
            usedBlood = false;
            bloodRecovery = User.RecoveryAmount;
            keys.Clear();
        }

    }
}
